package com.probox.common.entity.service.platfom.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.exception.PlatformException;
import com.probox.common.core.utils.DateUtils;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.core.utils.money.MoneyUtils;
import com.probox.common.entity.domain.platfom.vo.PlatformFreightMoudleVo;
import com.probox.common.entity.service.platfom.IPlatformFreightService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.platfom.PlatformFreightMoudleMapper;
import com.probox.common.entity.domain.platfom.PlatformFreightMoudle;
import com.probox.common.entity.service.platfom.IPlatformFreightMoudleService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 平台邮费模板Service业务层处理
 *
 * @author usoft
 * @date 2021-06-28
 */
@Service
@Transactional
public class PlatformFreightMoudleServiceImpl extends ServiceImpl<PlatformFreightMoudleMapper, PlatformFreightMoudle> implements IPlatformFreightMoudleService {
    @Autowired
    private PlatformFreightMoudleMapper platformFreightMoudleMapper;

    @Autowired
    private IPlatformFreightService iPlatformFreightService;

    /**
     * 查询平台邮费模板
     *
     * @param moudleUid 平台邮费模板ID
     * @return 平台邮费模板
     */
    @Override
    public PlatformFreightMoudle selectPlatformFreightMoudleById(Long moudleUid) {
        return this.getById(moudleUid);
    }

    /**
     * 后台获取邮费信息
     *
     * @param moudleUid
     * @return
     */
    @Override
    public PlatformFreightMoudleVo getVo(Long moudleUid) {
        PlatformFreightMoudleVo vo = new PlatformFreightMoudleVo();
        PlatformFreightMoudle byId = this.getById(moudleUid);
        if (ObjectUtil.isEmpty(byId)) throw new PlatformException("邮费模板数据有问题,请联系管理员！");
        BeanUtils.copyProperties(byId, vo);
        vo.setMoudleAmount(MoneyUtils.changeF2B2(byId.getMoudleAmount()));
        vo.setFreightInitFate(MoneyUtils.changeF2B2(byId.getFreightInitFate()));
        return iPlatformFreightService.getVo(vo);
    }

    /**
     * 查询平台邮费模板列表
     *
     * @param platformFreightMoudle 平台邮费模板
     * @return 平台邮费模板
     */
    @Override
    public List<PlatformFreightMoudle> queryList(PlatformFreightMoudle platformFreightMoudle) {
        Long platformId = SecurityUtils.getPlatformId();
        if (ObjectUtil.isEmpty(platformId)) throw new PlatformException("当前用户错误！");
        QueryWrapper<PlatformFreightMoudle> queryWrapper = Wrappers.query();
        queryWrapper.eq("PLATFORM_UID", platformId);
        if (StringUtils.isNotEmpty(String.valueOf(platformFreightMoudle.getMoudleName())) && platformFreightMoudle.getMoudleName() != null) {
            queryWrapper.eq("MOUDLE_NAME", platformFreightMoudle.getMoudleName());
        }
        queryWrapper.ne("DATA_STATUS", DataStatus.delete.getKey());
        if (StringUtils.isNotEmpty(String.valueOf(platformFreightMoudle.getDataStatus())) && platformFreightMoudle.getDataStatus() != null) {
            queryWrapper.eq("DATA_STATUS", platformFreightMoudle.getDataStatus());
        }

        return platformFreightMoudleMapper.selectList(queryWrapper);
    }

    /**
     * 新增和修改平台邮费模板
     *
     * @param vo 平台邮费模板
     * @return 结果
     */
    @Override
    public boolean insertPlatformFreightMoudle(PlatformFreightMoudleVo vo) {
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();
        Long platformId = SecurityUtils.getPlatformId();
        DateTime date = DateUtil.date();
        vo.setPlatformUid(platformId);
        if (ObjectUtil.isEmpty(vo)) throw new PlatformException("邮费模板数据有误,请重新添加！");
        PlatformFreightMoudle moudle = new PlatformFreightMoudle();
        BeanUtils.copyProperties(vo, moudle);
        if (ObjectUtil.isEmpty(vo.getMoudleUid())) {
            moudle.setCreateUid(userId);
            moudle.setCreateName(username);
            moudle.setCreateTime(date);
            moudle.setUpdateUid(userId);
            moudle.setUpdateName(username);
            moudle.setUpdateTime(date);
            moudle.setDataStatus(DataStatus.invalid.getKey());
            moudle.setMoudleAmount(MoneyUtils.changeY2B(moudle.getMoudleAmount()));
            moudle.setFreightInitFate(MoneyUtils.changeY2B(moudle.getFreightInitFate()));
            boolean save = this.save(moudle);
            if (!save) throw new PlatformException("添加失败,请稍后再试！");
            vo.setMoudleUid(moudle.getMoudleUid());
        } else {
            moudle.setUpdateUid(userId);
            moudle.setUpdateName(username);
            moudle.setUpdateTime(date);
            //moudle.setDataStatus(DataStatus.valid.getKey());
            moudle.setMoudleAmount(MoneyUtils.changeY2B(moudle.getMoudleAmount()));
            moudle.setFreightInitFate(MoneyUtils.changeY2B(moudle.getFreightInitFate()));
            this.updateById(moudle);
        }
        // 添加邮费设置
        iPlatformFreightService.insertPlatformFreight(vo);
        return true;

    }

    /**
     * 修改状态
     *
     * @param platformFreightMoudle 平台邮费模板
     * @return 结果
     */
    @Override
    public boolean updatePlatformFreightMoudle(PlatformFreightMoudle platformFreightMoudle) {
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();
        Date date = DateUtil.date();
        // 如果这个模板有效,就无效其他的模板
        if (DataStatus.valid.getKey().equals(platformFreightMoudle.getDataStatus())) {
            UpdateWrapper<PlatformFreightMoudle> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
            updateWrapper.ne("DATA_STATUS", DataStatus.delete.getKey());
            updateWrapper.set("DATA_STATUS", DataStatus.invalid.getKey());
            updateWrapper.set("UPDATE_UID", userId);
            updateWrapper.set("UPDATE_NAME", username);
            updateWrapper.set("UPDATE_TIME", date);
            this.update(updateWrapper);
        }
        platformFreightMoudle.setUpdateUid(userId);
        platformFreightMoudle.setUpdateName(username);
        platformFreightMoudle.setUpdateTime(date);
        return this.updateById(platformFreightMoudle);
    }

    /**
     * 批量删除平台邮费模板
     *
     * @param moudleUids 需要删除的平台邮费模板ID
     * @return 结果
     */
    @Override
    public boolean deletePlatformFreightMoudleByIds(Long[] moudleUids) {
        UpdateWrapper<PlatformFreightMoudle> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("MOUDLE_UID", Arrays.asList(moudleUids));
        updateWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_UID", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        this.update(updateWrapper);
        iPlatformFreightService.removeFreight(moudleUids);
        return true;
    }


}
